#!/usr/bin/env python

# vim: set ai et ts=4 sw=4:
import numpy as np
from PIL import Image
import sys
import os
import cv2
print (os.getcwd() )#打印出当前工作路径
'''if len(sys.argv) < 2:
    print("Usage: {} <image-file>".format(sys.argv[0]))
    sys.exit(1)

fname = sys.argv[1]
'''
#img1 = cv2.imread('04.png')

img_fn = os.path.join(os.getcwd(), '16')
name_flag=img_fn.split('img\\')[1].split('(')[0].split('.')[0]
img = Image.open(img_fn)  # RGB(默认)
#img=Image.open('E:\\STM32CUBEProjects\103_st7735\img\04.png')#Image.fromarray(np.uint8(img1))
#img = Image.open('E:\STM32CUBEProjects\103_st7735\img\01.png')
#if img.width != 128 or img.height != 128:
#    print("Error: 128x128 image expected");
#    sys.exit(2)

print("const uint16_t test_img_128x160_"+name_flag+"[][128] = {");

# print(img.getpixel( (0, 0) ))#(170, 182, 230, 255)(128, 255, 255)
for y in range(0, img.height):
    s = "{"
    for x in range(0, img.width):
        (r, g, b) = img.getpixel( (x, y) )
        color565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
        # for right endiness, so ST7735_DrawImage would work
        color565 = ((color565 & 0xFF00) >> 8) | ((color565 & 0xFF) << 8)
        s += "0x{:04X},".format(color565)
    s += "},"
    print(s)

print("};")
